<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    普通函数与箭头函数this指向问题 |  Yolin
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.png" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="Yolin" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-普通函数与箭头函数" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  普通函数与箭头函数this指向问题
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/02/22/%E6%99%AE%E9%80%9A%E5%87%BD%E6%95%B0%E4%B8%8E%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0/" class="article-date">
  <time datetime="2020-02-22T10:56:02.000Z" itemprop="datePublished">2020-02-22</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/JavaScript/">JavaScript</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">989字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">4分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h2 id="一、普通函数"><a href="#一、普通函数" class="headerlink" title="一、普通函数"></a>一、普通函数</h2><h3 id="举例"><a href="#举例" class="headerlink" title="举例"></a>举例</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a=<span class="number">5</span>;  <span class="comment">//全局变量</span></span><br><span class="line"><span class="keyword">var</span> obj=&#123;</span><br><span class="line">    a:<span class="number">2</span>,</span><br><span class="line">    show:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="keyword">this</span>.a);</span><br><span class="line">      &#125;   </span><br><span class="line">&#125;</span><br><span class="line">obj.show();   <span class="comment">//2</span></span><br><span class="line"><span class="keyword">var</span> obj2=obj;</span><br><span class="line">obj2.a=<span class="number">8</span>;</span><br><span class="line">obj2.show();    <span class="comment">//8</span></span><br><span class="line"><span class="keyword">var</span> obj3=obj.show;</span><br><span class="line">obj3();      <span class="comment">//5</span></span><br></pre></td></tr></table></figure>
<h3 id="发现"><a href="#发现" class="headerlink" title="发现"></a>发现</h3><p>三次调用show方法，我们可以发现：</p>
<ul>
<li><strong>当直接调用对象自己内的方法时，this指向调用show方法的对象即obj；</strong></li>
<li><strong>而将obj赋给新对象obj2时，再调用show时this就指向调用者obj2</strong>；<a id="more"></a></li>
<li><strong>将obj.show赋给一个新对象obj3，通过obj3调用show函数，通过obj3调用show函数，此时的this指向window；</strong></li>
</ul>
<h3 id="普通函数this指向问题"><a href="#普通函数this指向问题" class="headerlink" title="普通函数this指向问题"></a>普通函数this指向问题</h3><p>再考虑一下下面的情况：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj4 = &#123;</span><br><span class="line">  a : <span class="number">1</span>,</span><br><span class="line">  func: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="keyword">this</span>.a);  <span class="comment">//1</span></span><br><span class="line">  setTimeout(</span><br><span class="line">      <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="built_in">console</span>.log(<span class="keyword">this</span>.a)&#125;,<span class="number">3000</span>)</span><br><span class="line">       &#125;</span><br><span class="line">&#125;</span><br><span class="line">obj4.func(); <span class="comment">//3秒后输出全局变量中的5</span></span><br></pre></td></tr></table></figure>
<p>obj4.func()中this可得到当前对象中的a，而setTimeout中要执行的方法是属于 window 的 method，因此它的this指向还是window对象</p>
<p><strong>普通函数中，内层函数不能从外层函数中继承this的值，在内层函数中，this会是window或者undefined</strong>（取决于是否使用严格模式），可以设置一个临时变量用来将外部的this值导入到内部函数中，另外就是在内部函数执行.bind(this)</p>
<h3 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h3><h5 id="1、var-that-this"><a href="#1、var-that-this" class="headerlink" title="1、var that=this"></a>1、var that=this</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj4 = &#123;</span><br><span class="line">  a : <span class="number">1</span>,</span><br><span class="line">  func: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      <span class="keyword">var</span> that=<span class="keyword">this</span>;</span><br><span class="line">  setTimeout(</span><br><span class="line">      <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="built_in">console</span>.log(that.a)&#125;,<span class="number">3000</span>)</span><br><span class="line">       &#125;</span><br><span class="line">&#125;</span><br><span class="line">obj4.func();   <span class="comment">//1</span></span><br></pre></td></tr></table></figure>
<h5 id="setTimeout语法"><a href="#setTimeout语法" class="headerlink" title="setTimeout语法"></a><em>setTimeout语法</em></h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.setTimeout(code,delay);</span><br><span class="line"><span class="comment">//setTimeout是Window对象的方法(可省略window)</span></span><br></pre></td></tr></table></figure>

<p>setTimeout(匿名函数, time)这种形式下，匿名函数中的变量也需要根据上下文来判断，<br>前面的this.a毫无疑问是全局中的a，而有了that变量，因为func作用域使得window.setTimeout能获取到当前对象（而非window对象），故a为1</p>
<h5 id="2、通过bind-来绑定this"><a href="#2、通过bind-来绑定this" class="headerlink" title="2、通过bind()来绑定this"></a>2、通过bind()来绑定this</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj5 = &#123;</span><br><span class="line">     a : <span class="number">7</span>,</span><br><span class="line">     func: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">         <span class="built_in">console</span>.log(<span class="keyword">this</span>.a);</span><br><span class="line">     setTimeout(</span><br><span class="line">         <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="built_in">console</span>.log(<span class="keyword">this</span>.a)&#125;.bind(<span class="keyword">this</span>),<span class="number">3000</span>)</span><br><span class="line">          &#125;</span><br><span class="line">   &#125;</span><br><span class="line">   obj5.func()  <span class="comment">//7</span></span><br></pre></td></tr></table></figure>

<p>调用 <strong>匿名函数.bind(this)</strong> 会创建一个与该函数具有相同函数体和作用域的函数，但是在这个新函数中，this将永久地被绑定到了bind的第一个参数（这里只有this），无论这个函数是如何被调用的。bind绑定的参数只生效一次。</p>
<hr>
<h2 id="二、箭头函数"><a href="#二、箭头函数" class="headerlink" title="二、箭头函数"></a>二、箭头函数</h2><p>ES6标准新增了一种新的函数：Arrow Function（箭头函数）。</p>
<p>箭头函数相当于匿名函数，并且简化了函数定义。箭头函数有两种格式</p>
<ul>
<li>++只包含一个表达式，连{ … }和return都省略掉了。++</li>
<li>++还有一种可以包含多条语句，这时候就不能省略{ … }和return++</li>
</ul>
<p><strong>箭头函数中的this始终指向其父级作用域中的this，任何方法都改变不了其指向</strong></p>
<h3 id="栗子"><a href="#栗子" class="headerlink" title="栗子"></a>栗子</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj6= &#123;</span><br><span class="line">  a : <span class="number">101</span>,</span><br><span class="line">  func:<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="keyword">this</span>.a) </span><br><span class="line">  &#125;,</span><br><span class="line">  func1:<span class="function"><span class="params">()</span>=&gt;</span>&#123; </span><br><span class="line">     <span class="keyword">var</span> b=<span class="function"><span class="params">()</span>=&gt;</span><span class="keyword">this</span>.a</span><br><span class="line">     <span class="built_in">console</span>.log(b())                               </span><br><span class="line">  &#125;,</span><br><span class="line">  func2:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      <span class="keyword">var</span> b=<span class="function"><span class="params">()</span>=&gt;</span><span class="keyword">this</span>.a</span><br><span class="line">      <span class="built_in">console</span>.log(b())</span><br><span class="line">  &#125;,     </span><br><span class="line">&#125;</span><br><span class="line">obj6.func()  <span class="comment">//全局变量5</span></span><br><span class="line">obj6.func1(); <span class="comment">//5</span></span><br><span class="line">obj6.func2()   <span class="comment">//101</span></span><br></pre></td></tr></table></figure>
<h3 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h3><p>obj6.func()中的this会指向父级作用域的this也就是与obj6有相同的this指向，为window</p>
<p><strong>对于方法，普通函数中的this总是指向它的调用者</strong>，所以obj6.func2()中的this指向为obj6的指向，所以a为101</p>
<hr>
<h2 id="三、两者差异"><a href="#三、两者差异" class="headerlink" title="三、两者差异"></a>三、两者差异</h2><p>箭头函数和普通函数相比，有以下几点差异：</p>
<ol>
<li><p><strong>箭头函数没有 this，它会从自己的作用域链的上一层继承 this（因此无法使用 apply / call / bind 进行绑定 this 值</strong>）</p>
</li>
<li><p><strong>函数体内的 this对象，就是定义时所在的对象，而不是使用时所在的对象。</strong></p>
</li>
<li><p><strong>不可以使用 arguments对象，该对象在函数体内不存在。如果要用，可以用 rest 参数代替。</strong></p>
</li>
<li><p><strong>不可以使用 yield 命令，因此箭头函数不能用作 Generator 函数。</strong></p>
</li>
<li><p><strong>不可以使用 new 命令，因为</strong>：</p>
</li>
</ol>
<ul>
<li><em>没有自己的 this，无法调用 call，apply。</em></li>
<li><em>没有 prototype 属性 ，即指向 undefined；</em></li>
</ul>
<hr>
<h2 id="四、参考资料"><a href="#四、参考资料" class="headerlink" title="四、参考资料"></a>四、参考资料</h2><p><a href="https://www.jianshu.com/p/e5fe25edd78a" target="_blank" rel="noopener">https://www.jianshu.com/p/e5fe25edd78a</a><br><a href="https://www.jianshu.com/p/acbd5a9b0211" target="_blank" rel="noopener">https://www.jianshu.com/p/acbd5a9b0211</a><br><a href="https://www.liaoxuefeng.com/wiki/1022910821149312/1031549578462080" target="_blank" rel="noopener">https://www.liaoxuefeng.com/wiki/1022910821149312/1031549578462080</a></p>

      
      <!-- reward -->
      
    </div>
      <!-- copyright -->
      
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/" rel="tag">JavaScript</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/this/" rel="tag">this</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%99%AE%E9%80%9A%E5%87%BD%E6%95%B0/" rel="tag">普通函数</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0/" rel="tag">箭头函数</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/03/07/Vue%E4%B8%AD%E4%BD%BF%E7%94%A8axios%E5%8F%91%E9%80%81ajax%E8%AF%B7%E6%B1%82/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Vue中使用axios发送ajax请求
          
        </div>
      </a>
    
    
      <a href="/2020/02/20/%E7%BD%91%E7%BA%A2%E9%9D%A2%E8%AF%95%E9%A2%98/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">网红面试题[&#39;1&#39;,&#39;2&#39;,&#39;3&#39;].map(parseInt)</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        张金
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
        <img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/beian.png">
        <a href="https://beian.miit.gov.cn"  style="color:#f72b07" target="_blank">陕ICP备20002449号</a>
      </li>

    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Yolin"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/playlist">歌单</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/video">视频</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友情链接</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['肠断月明红豆蔻,','月似当时,','人似当时否？'],
    startDelay: 1500,
    typeSpeed: 200,
    loop: true,
    backSpeed: 40,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>